﻿@page "/user/space/{Id}/{Type}"
@page "/user/space/{Id}"

@inject IPageModelCatche<UserSpaceViewModel> _pageModelCatche
@inject NavigationManager NavigationManager
@implements IDisposable

<CnGalWebSite.ProjectSite.Shared.Components.Shared.TitleCard Title="@Model?.UserInfo?.Name" />

@if (Model != null)
{
    <CnGalWebSite.ProjectSite.Shared.Components.Users.UserInfoCard Model="Model.UserInfo" />

    @if (Model.UserInfo.Type == UserType.Person)
    {
        <MDCard Class="mb-4">
            <MTabs Value="Model.TabIndex" IconsAndText Grow CenterActive ValueChanged="TabValueChanged">

                 <MTab Value="2">
                     橱窗<MIcon>@IconType.Stall.ToIconString()</MIcon>
                 </MTab>
                 <MTab Value="1">
                     作品<MIcon>@IconType.Shape.ToIconString()</MIcon>
                 </MTab>
                 <MTab Value="0">
                     企划<MIcon>@IconType.Project.ToIconString()</MIcon>
                 </MTab>
                 <MTab Value="3">
                     认证<MIcon>@IconType.Certification.ToIconString()</MIcon>
                 </MTab>
                    <MTab Value="4">
                        应征<MIcon>@IconType.Flag.ToIconString()</MIcon>
                    </MTab>

             </MTabs>
         </MDCard>
    }
    else
    {
        <MDCard Class="mb-4">
            <MTabs Value="Model.TabIndex" IconsAndText Grow CenterActive ValueChanged="TabValueChanged">
                 <MTab Value="0">
                     企划<MIcon>@IconType.Project.ToIconString()</MIcon>
                 </MTab>
                 <MTab Value="1">
                     作品<MIcon>@IconType.Shape.ToIconString()</MIcon>
                 </MTab>
                 <MTab Value="2">
                     橱窗<MIcon>@IconType.Stall.ToIconString()</MIcon>
                 </MTab>
                 <MTab Value="3">
                     认证<MIcon>@IconType.Certification.ToIconString()</MIcon>
                 </MTab>
                    <MTab Value="4">
                        应征<MIcon>@IconType.Flag.ToIconString()</MIcon>
                    </MTab>
             </MTabs>
         </MDCard>
    }

    <MTabsItems Value="Model.TabIndex">
        <MTabItem Value="0">
            <CnGalWebSite.ProjectSite.Shared.Components.Users.MainCards.ProjectGroupCard Model="Model.Projects" />
        </MTabItem>
        <MTabItem Value="1">
            @if(Model.Texts.Any()||Model.Images.Any()||Model.Images.Any())
            {
                            <CnGalWebSite.ProjectSite.Shared.Components.Users.MainCards.ImageCard Model="Model" />
            <CnGalWebSite.ProjectSite.Shared.Components.Users.MainCards.AudioCard Model="Model" />
                <CnGalWebSite.ProjectSite.Shared.Components.Users.MainCards.TextGroupCard Model="Model" />

            }
            else
            {
                <NotFoundCard Fill/>
            }
        </MTabItem>
        <MTabItem Value="2">
            <CnGalWebSite.ProjectSite.Shared.Components.Users.MainCards.StallGroupCard Model="Model.Stalls" />
        </MTabItem>
        <MTabItem Value="3">
            <CnGalWebSite.ProjectSite.Shared.Components.Users.MainCards.CertificationCard Model="Model"/>
        </MTabItem>
        <MTabItem Value="4">
            <CnGalWebSite.ProjectSite.Shared.Components.Users.MainCards.PositionGroupCard Model="Model.Positions" />
        </MTabItem>
    </MTabsItems>

}
else
{
    <CnGalWebSite.Components.Progressings.ProgressingCard Page />
}


@code {
    [Parameter]
    public string Id { get; set; }

    [Parameter]
    public string Type { get; set; }

    UserSpaceViewModel Model;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    protected override async Task OnInitializedAsync()
    {
        NavigationManager.LocationChanged -= OnLocationChanged;
        NavigationManager.LocationChanged += OnLocationChanged;

        await Refresh();
    }

    public async Task Refresh()
    {
        try
        {
            Model = await _pageModelCatche.GetCache($"api/user/GetUserSpace?id={Id}");
            if (Type == "0")
            {
                Model.UserInfo.Type = UserType.Person;
            }
            else if (Type == "1")
            {
                Model.UserInfo.Type = UserType.Organization;
            }
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取用户空间数据失败");
        }
    }

    public async void OnLocationChanged(object sender, LocationChangedEventArgs args)
    {
        await Refresh();
    }


    public void TabValueChanged(StringNumber index)
    {
        Model.TabIndex = index.ToInt32();
    }

    public void Dispose()
    {
        NavigationManager.LocationChanged -= OnLocationChanged;
        GC.SuppressFinalize(this);
    }

}
